import OpenAI from "openai";

export async function punctuateText(text) {
    try {
        const openai = new OpenAI({
            apiKey: "sk-ab1cdf9501ec4f669046d1dbf123ac66", // 请替换为你的实际 API Key
            baseURL: "https://dashscope.aliyuncs.com/compatible-mode/v1",
            dangerouslyAllowBrowser: true
        });

        const completion = await openai.chat.completions.create({
            model: "qwen-plus",
            messages: [
                { role: "system", content: "You are a helpful assistant." },
                { role: "user", content: `请为以下文本添加标点：${text}` }
            ],
        });

        return completion.choices[0].message.content;
    } catch (error) {
        console.error(`错误信息：${error}`);
        console.log("请参考文档：https://help.aliyun.com/zh/model-studio/developer-reference/error-code");
        throw error;
    }
}

export async function segmentText(text) {
    try {
        const openai = new OpenAI({
            apiKey: "sk-ab1cdf9501ec4f669046d1dbf123ac66", // 请替换为你的实际 API Key
            baseURL: "https://dashscope.aliyuncs.com/compatible-mode/v1",
            dangerouslyAllowBrowser: true
        });

        const completion = await openai.chat.completions.create({
            model: "qwen-plus",
            messages: [
                { role: "system", content: "You are a helpful assistant." },
                { role: "user", content: `请为以下文本进行分词：${text}，分词后词与词之间用空格分开，而且不要有任何其他内容` }
            ],
        });

        // 假设返回的内容是分词后的文本，词与词之间用空格分开
        return completion.choices[0].message.content;
    } catch (error) {
        console.error(`错误信息：${error}`);
        console.log("请参考文档：https://help.aliyun.com/zh/model-studio/developer-reference/error-code");
        throw error;
    }
}